package com.xiaotou.domain.impl;

import java.util.Date;

import com.xiaotou.db.pojo.ActingCareer;
import com.xiaotou.db.pojo.ActionTask;
import com.xiaotou.db.pojo.AppUser;
import com.xiaotou.domain.Engine;
import com.xiaotou.domain.Prey;
import com.xiaotou.domain.Roles;
import com.xiaotou.domain.Thief;
import com.xiaotou.domain.SkillLevel;
import com.xiaotou.weibo.WeiboUser;

public class ThiefImpl implements Thief {

	private Engine engine;

	private WeiboUser actor;

	public ThiefImpl(WeiboUser actor) {
		this.actor = actor;
	}

	@Override
	public WeiboUser getActor() {
		return actor;
	}

	@Override
	public SkillLevel getSkillLevel() {
		return engine.getSkillLevel(this);
	}

	@Override
	public ActionTask target(Prey prey) {
		final ActionTask action = new ActionTask();
		action.setCreateTime(new Date());
		action.setThief(actor.getWbUser());
		action.setPrey(prey.getActor().getWbUser());
		action.setPartnersThreshold(engine.getPartnersThreshold(this, prey));
		action.setAvailableMoney(prey.getAvailableMoney());
		return action;
	}

	public void setEngine(Engine engine) {
		this.engine = engine;
	}

	@Override
	public void action(ActionTask actionTask) {
		actionTask.setActionTime(new Date());
		actionTask.setState(ActionTask.ACTION_STATE);
	}

	@Override
	public void done(ActionTask actionTask) {
		actionTask.setState(ActionTask.DONE_STATE);
		actionTask.setDoneTime(new Date());
		final AppUser user = actionTask.getThief().getAppUser();
		user.setMoney(user.getMoney() + actionTask.getGotMoney());
		ActingCareer actingCareer = user.getActingCareers().get(
				Roles.Thrief.getValue());
		if (actingCareer == null) {
			actingCareer = new ActingCareer();
			actingCareer.setAppUser(user);
			actingCareer.setStartTime(new Date());
			user.getActingCareers().put(Roles.Thrief.getValue(), actingCareer);
		}
		actingCareer.setActionCount(actingCareer.getActionCount() + 1);
	}

	@Override
	public ActingCareer getActingCareer() {
		final AppUser appUser = this.actor.getWbUser().getAppUser();
		if (appUser != null) {
			return appUser.getActingCareers().get(Roles.Thrief.getValue());
		} else {
			return null;
		}
	}
}
